//
//  TSAuditStatusListTableView.m
//  chart
//
//  Created by xu on 15/9/14.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSAuditStatusListTableView.h"
#import "TSMsgAuditStatusModel.h"

#define STATISTICS_CELL_HEGITH 50.f
#define STATISTICS_BTN_WIDTH 50.f
#define SEARCH_LABEL_WIDTH  50.f
#define SEARCH_LABEL_HEIGHT 35.f
#define SEARCH_ROW_GAP 10.f
@implementation TSAuditStatusListTableView{
    TSMsgAuditStatusModel *auditStatusModel;
    NSMutableArray *dataArray;
    UILabel * lb_time;
    UILabel * lb_title;
}

@synthesize messageId,timeStr,title;

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style timeStr:(NSString*)timeStr title:(NSString*)title{
    if (self=[super initWithFrame:frame style:style]) {
        self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        self.tableFooterView = [UIView new];
        
        self.dataSource = self;
        self.delegate = self;
        self.timeStr = timeStr;
        self.title = title;
        self.tableHeaderView = [self createHeaderView];
        auditStatusModel = [[TSMsgAuditStatusModel alloc] init];
    }
    
    return self;
}

-(UIView *)createHeaderView
{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, currentDeviceWidth, SEARCH_LABEL_HEIGHT*3)];
    [self addTimeLabel:view];
    [self addTitleLable:view];
    return view;
}

- (void)addTimeLabel:(UIView *)view
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, MARGIN_TOP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"时间:"];
    [view addSubview:label];
    
    lb_time = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, CGRectGetMinY(label.frame), currentDeviceWidth-2*MARGIN_LEFT-SEARCH_LABEL_WIDTH-10.f, SEARCH_LABEL_HEIGHT) Title:timeStr];
    [view addSubview:lb_time];
}
- (UILabel *)createLabel:(CGRect)frame Title:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:title];
    [label setFont:FONT(12.f)];
    
    return label;
}

- (void)addTitleLable:(UIView *)view
{
    UILabel *label = [self createLabel:CGRectMake(MARGIN_LEFT, SEARCH_LABEL_HEIGHT+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:@"标题:"];
    [view addSubview:label];
    
    lb_title = [self createLabel:CGRectMake(CGRectGetMaxX(label.frame)+10.f, SEARCH_LABEL_HEIGHT+SEARCH_ROW_GAP, SEARCH_LABEL_WIDTH, SEARCH_LABEL_HEIGHT) Title:title];
    [view addSubview:lb_title];
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cell_identify = @"cellIdentify";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cell_identify];
    }
    
    if (dataArray && dataArray.count > indexPath.row) {
        TSMsgAuditStatusItemModel *model = [dataArray objectAtIndex:indexPath.row];
        cell.textLabel.text = model.name;
        [cell.textLabel setFont:FONT(12.f)];
        cell.detailTextLabel.text = [TSTools getSendStatusTypeString:model.status.integerValue];
        [cell.detailTextLabel setFont:FONT(12.f)];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (dataArray) {
        return [dataArray count];
    }
    
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return STATISTICS_CELL_HEGITH;
}

- (void)headerRequestData
{
    WS(weakSelf);
    self.pageIndex = 0;
    auditStatusModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize), TSDictionaryKeyDefine_Id:messageId?:@""};
    [auditStatusModel fetchWithPath:kRequestUrl_SendMessage_outList_auditorList completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        if (strongSelf->auditStatusModel.list.count > 0) {
            strongSelf.footer.hidden = NO;
            
            strongSelf->dataArray = strongSelf->auditStatusModel.list;
            
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

- (void)footerRequestData
{
    WS(weakSelf);
    self.pageIndex++;
    auditStatusModel.requestParams = @{TSDictionaryKeyDefine_PageIndex:@(self.pageIndex), TSDictionaryKeyDefine_PageSize:@(self.pageSize), TSDictionaryKeyDefine_Id:messageId?:@""};
    [auditStatusModel fetchWithPath:kRequestUrl_SendMessage_outList_auditorList completionHandler:^(BOOL isSucceeded, NSError *error) {
        [weakSelf.header endRefreshing];
        [weakSelf.footer endRefreshing];
        
        if (!weakSelf) {
            return;
        }
        __strong typeof (weakSelf)strongSelf = weakSelf;
        
        if (strongSelf->auditStatusModel.list.count == 0) {
            strongSelf.footer.hidden = YES;
        }
        else{
            [strongSelf->dataArray addObjectsFromArray:strongSelf->auditStatusModel.list];
            
            [weakSelf reloadData];
        }
    } NoProgress:NO];
}

@end
